package com.example.studentinformation.controllor;

import com.example.studentinformation.dao.AdminMapper;
import com.example.studentinformation.dao.LoginMapper;
import com.example.studentinformation.dao.MemberMapper;
import com.example.studentinformation.dao.OrderMapper;
import com.example.studentinformation.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author 10418
 */

@RequestMapping("stu")
@Controller

public class HtmlCon {

    @Autowired
    MemberMapper memberMapper ;
    @Autowired
    AdminMapper adminMapper;

    @Autowired
    LoginMapper loginMapper;

    @Autowired
    OrderMapper orderMapper;


    @RequestMapping("memberlist")
    @ResponseBody
    public List<Member> list(){
        List<Member> list = memberMapper.list();
        return list;
    }

    @RequestMapping("member-list")
    public String memberlist(ModelMap map) {
        List<Member> list = memberMapper.list();
        map.put("memberlist",list);
        map.put("membersize",list.size());
        return "student/member-list";
    }
    @RequestMapping("member-delect/{mail}")
    public String    memberdelect(@PathVariable("mail") String xxx) {
        memberMapper.delect(xxx);
        return "redirect:/stu/member-list";
    }
    @PostMapping("member-save")
    public  String  membersave(Member member) {
        memberMapper.add(member);
        return "redirect:member-list";
    }


    @RequestMapping("member-update/{mail}/{name}/{age}/{sex}/{phone}/{address}")

    public String member_updata(@PathVariable("mail") String mail,
                                @PathVariable("name") String name,
                                @PathVariable("age") String age,
                                @PathVariable("sex") String sex,
                                @PathVariable("phone") String phone,
                                @PathVariable("address") String address){
        return "student/member-update";
    }

    @PostMapping("member-edit")
    public  String  memberedit(Member member) {
        memberMapper.edit(member);
        return "redirect:member-list";
    }








    @RequestMapping("adminlist")
    @ResponseBody
    public List<Admin> admin_list(){
        List<Admin> list = adminMapper.list();
        return list;
    }

    @RequestMapping("admin-list")
    public String adminlist(ModelMap map) {
        List<Admin> list = adminMapper.list();
        map.put("adminlist",list);
        map.put("adminsize",list.size());
        return "student/admin-list";
    }


    @RequestMapping("admin-update/{phone}/{name}/{email}/{password}")

    public String updata(@PathVariable("phone") String phone,
                         @PathVariable("name") String name,
                         @PathVariable("email") String email,
                         @PathVariable("password") String password
                         ){
        return "student/admin-update";
    }


    @PostMapping("admin-edit")
    public  String  adminedit(Admin admin) {
        adminMapper.edit(admin);
        return "redirect:admin-list";
    }

    @PostMapping("admin-save")
    public  String  adminsave(Admin admin) {
        adminMapper.add(admin);
        return "redirect:admin-list";
    }

    @RequestMapping("admin-delect/{phone}")
    public String    admindelect(@PathVariable("phone") String xxx) {
        adminMapper.delect(xxx);
        return "redirect:/stu/admin-list";
    }





    @RequestMapping("orderlist")
    @ResponseBody
    public List<Order> order_list(){
        List<Order> list = orderMapper.list();
        return list;
    }

    @RequestMapping("order-list")
    public String   orderlist(ModelMap map) {
        List<Order> list = orderMapper.list();
        map.put("orderlist",list);
        map.put("ordersize",list.size());
        return "student/order-list";
    }


    @PostMapping("order-edit")
    public  String  oreredit(Order order) {
        orderMapper.edit(order);
        return "redirect:order-list";
    }



    @RequestMapping("order-delect/{num}")
    public String    orderdelect(@PathVariable("num") String xxx) {
        orderMapper.delect(xxx);
        return "redirect:/stu/order-list";
    }

    @PostMapping("order-save")
    public  String  ordersave(Order order) {
        orderMapper.add(order);
        return "redirect:order-list";
    }







    @PostMapping("admin-login")
    public  String  loginlogin(AdminLogin adminlogin, Map<String, Object> map) {
        if(loginMapper.select_admin(adminlogin.getPhone())!=null){
            AdminLogin adminLogin1=loginMapper.select_admin(adminlogin.getPhone());
            if(adminLogin1.getPassword().equals(adminlogin.getPassword())){
                return "index";
            }else {
                map.put("msg", "用户名密码错误");
                return "redirect:login";
            }
        }
        return "student/login";
    }



    @RequestMapping("login")
    public String login() {
        return "student/login";
    }

    @RequestMapping("welcome")
    public String welcome() {
        return "student/welcome";
    }

    @RequestMapping("index")
    public String index() {
        return "/index";
    }
}

